package top.jiangqiang.qyblog.core.handler;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.nio.charset.StandardCharsets;

/**
 * jdk原生序列化和对象之间转换
 *
 * @author JiangQiang
 * @since 2022-09-10
 */
@Slf4j
@MappedTypes({Object.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class JdkTypeHandler extends AbstractJsonTypeHandler<Object> {

    @Override
    protected Object parse(String str) {
        return ObjectUtil.deserialize(str.getBytes(StandardCharsets.ISO_8859_1));
    }

    @Override
    protected String toJson(Object obj) {
        return new String(ObjectUtil.serialize(obj), StandardCharsets.ISO_8859_1);
    }
}
